<template>
  <div class="workflow_input">
    <div class="workflow_input_title">{{field ? field.alia : ''}}</div>
    <div class="workflow_input_container">
      <input class="workflow_input_input" v-show="!selectData" type="number" :value="value" @input="input"/>
      <input class="workflow_input_input" v-show="selectData" type="number" @input="input"/>
      <div class="workflow_input_select" v-if="selectData">{{selectData.alia}}</div>
      <button-popover :output="output" @change="handleSelectChange"></button-popover>
    </div>
  </div>
</template>

<script>
import ButtonPopover from './buttonPopover.vue'
export default {
	props: {
		value: {
			default: ""
		},
    field: Object,
		output: {
			default: []
		},
	},
  components:{
    ButtonPopover
  },
	data(){
		return{
      selectData: null
		}
	},
	methods:{
		input(event){  
      this.selectData = null
			this.$emit('input',event.target.value);
		},
    handleSelectChange(data){
      this.selectData = data
      this.value = ''
			this.$emit('input', `{{${data.parentNodeId}.outputs.${data.parentKey}.${data.name}}}`);
    },
    findSelectItem(str){
      let split = str.split('.')
      for (let i = 0; i < this.output.length; i++) {
        if(this.output[i].nodeId === split[0]){
          this.selectData = this.output[i].outputs.find(c => c.name === split[split.length - 1])
        }
      }
    }
	},
  mounted(){
    let re = /^{{(\w+(?:\.\w+)+)}}$/
    if(re.test(this.value)){
      this.findSelectItem(re.exec(this.value)[1])
    }
  },
  computed:{
  }
}

</script>

<style lang="less">
.workflow_input{
    // display: flex;
    // align-items: center;
    .workflow_input_title{
      margin-bottom: 5px;
    }

    .workflow_input_container{
      display: flex;
      align-items: center;
      position: relative;

      .workflow_input_input{
        background-color: #fff;
        background-image: none;
        border: 1px solid #d9d9d9;
        border-radius: 4px;
        flex: 1 1 auto;
        color: rgba(0, 0, 0, 0.65);
        overflow: hidden;
        text-overflow: ellipsis;
        height: 32px;
        padding: 0 10px;
      }

      .workflow_input_select{
        margin: 0 10px;
        padding: 0 10px;
        position: absolute;
        background-color: rgba(127, 255, 212, 0.6);
      }
    }
}
</style>